package com.tripspins.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.tripspins.app.MainActivity;
import com.tripspins.app.LoginActivity;
import com.tripspins.app.MapActivity;
import com.tripspins.app.NewPinActivity;
import com.tripspins.app.R;
import com.tripspins.app.services.BackgroundSyncService;

/**
 * Created by João.Rutkoski on 12/10/13.
 */
public class DrawerMenuAdapter   extends BaseAdapter {

    Activity activity;
    String[] menuOptions;
    LayoutInflater inflater;
    boolean isOnTheRoad;
    boolean isAuthenticated;

    public DrawerMenuAdapter(Activity a, String[] menuOptions, boolean isOnTheRoad, boolean isAuthenticated)
    {
        this.activity = a;
        this.menuOptions = menuOptions;
        this.isOnTheRoad = isOnTheRoad;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.isAuthenticated = isAuthenticated;
    }

    @Override
    public int getCount() {
        return menuOptions.length;
    }

    @Override
    public Object getItem(int i) {
        return menuOptions[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        if(view == null)
            v = inflater.inflate(R.layout.drawer_menu_item,null);
        v.setClickable(true);
        v.setFocusable(true);
        TextView txtDrawerMenuItem = (TextView)v.findViewById(R.id.drawer_menu_item_text);
        txtDrawerMenuItem.setText((String)getItem(i));
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DrawerLayout drawerLayout = (DrawerLayout)activity.findViewById(R.id.drawer_layout);
                drawerLayout.closeDrawer((activity.findViewById(R.id.left_drawer)));
            }
        };
        switch (i)
        {
            case 0: //Current Trip
                if(isOnTheRoad){
                    if(!( activity instanceof MapActivity)){ //if already on MapActivity, do nothing
                        clickListener = new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                final Intent intentMapActivity = new Intent(activity,MapActivity.class);
                                activity.startActivity(intentMapActivity);
                            }
                        };
                    }
                }
                else
                {
                    if(!( activity instanceof NewPinActivity)){ //if already on NewPinActivity, do nothing
                        clickListener = new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                final Intent intentNewTripActivity = new Intent(activity,NewPinActivity.class);
                                activity.startActivity(intentNewTripActivity);
                            }
                        };
                    }
                }
                break;
            case 1: //My Trips
                if(!( activity instanceof MainActivity)){ //if already on ListTripsActivity, do nothing
                    clickListener = new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final Intent intentListTripsActivity = new Intent(activity,MainActivity.class);
                            activity.startActivity(intentListTripsActivity);
                        }
                    };
                }
                break;
            case 2:
                clickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final Intent intentLoginActivity = new Intent(activity, LoginActivity.class);
                        activity.startActivity(intentLoginActivity);
                    }
                };
                break;
            case 3:
                clickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(!isAuthenticated)
                            Toast.makeText(activity,activity.getString(R.string.login_warning),Toast.LENGTH_SHORT).show();
                        else
                        {
                            Intent backgroundSyncServiceIntent = new Intent(activity, BackgroundSyncService.class);
                            activity.startService(backgroundSyncServiceIntent);
                        }}};
                break;
            default:
                clickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                };
                break;

        }
        v.setOnClickListener(clickListener);

        return v;
    }
}
